<!DOCTYPE html>
<html>
<head>   
    <title>roles</title>   
    <script type="text/javascript" src="../javascript/page/Child.js"></script>
    <style type="text/css">
        html, body {
            font-size: 12px;  background-color: #f1f1f1;
        }
    </style>
</head>
<body onload="docLoaded();">
    <div id="queryForm" class="title_bar">
        <template>          
            <div class="query_form">
                <el-form :inline="true" :model="searchData" class="demo-form-inline">                    
                    <el-form-item label="RoleName">
                        <el-input v-model="searchData.roleName" placeholder="Role name"></el-input>
                    </el-form-item>        
                    <el-form-item>
                        <el-button type="primary" @click="onSubmit">Query</el-button>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="handleAdd" icon="el-icon-plus" plain>Add</el-button>
                    </el-form-item>
                </el-form>
            </div>       
            <div class="hline"></div>
            <div class="panel">              
                <el-table :data="tableData" style="width: 100%" max-height="660">
                    <el-table-column prop="id" label="ID" min-width="5%"></el-table-column>
                    <el-table-column prop="roleName" label="Role Name" min-width="15%"></el-table-column>
                    <el-table-column prop="roleDescription" label="Description" min-width="40%"></el-table-column>                 
                    <el-table-column prop="statusName" label="Status" min-width="10%"></el-table-column>
                    <el-table-column label="Operation" min-width="30%">
                        <template slot-scope="scope">                                      
                            <el-button @click="handleEdit(scope.row)" type="text" size="small">编辑</el-button>
                        </template>
                    </el-table-column>
                </el-table>                 
            </div>   
            <div class="status_bar" style="text-align: left;">
                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="searchData.pageNum"
                    :page-sizes="[5, 10, 15]"
                    :page-size="searchData.pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="pageInfo.total">
                </el-pagination> 
            </div>
            <el-dialog :title="dialogTitle" :visible.sync="editDialogFormVisible"> 
                <el-form :model="dialogForm"> 
                    <el-form-item label="Role Name" :label-width="formLabelWidth">
                        <el-input v-model="dialogForm.roleName" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="Description" :label-width="formLabelWidth">
                        <el-input v-model="dialogForm.roleDescription" autocomplete="off" type="textarea"></el-input>
                    </el-form-item>                  
                    <el-form-item label="Status" :label-width="formLabelWidth">
                        <el-select v-model="dialogForm.status" placeholder="please select status">
                            <el-option v-for="item in statusList"
                            :key="item.status"
                            :label="item.name"
                            :value="item.status">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="editDialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="editSubmit">确 定</el-button>
                </div>                
              </el-dialog>
        </template>
    </div>
    <script type="text/javascript">
        var router = startPage();
        function docLoaded() {           
            var queryApp = new Vue({el: '#queryForm', data: {                                
                    searchData:{ 
                        roleName: "", 
                        pageNum:1,
                        pageSize:5
                    },
                    pageInfo:{
                        totalPage:0,
                        total:0
                    },
                    tableData:[],
                    statusList:[{status:0,name:"normal"},{status:1,name:"disable"}],
                    formLabelWidth: '120px',
                    editDialogFormVisible:false,                    
                    dialogTitle: "Edit",
                    dialogMode: 0,
                    dialogForm:{                      
                        id:0,
                        roleName:"",
                        roleDescription:"",                             
                        status: 0
                    }
                },
                methods: {     
                    onSubmit:function(event){
                        this.searchData.pageNum = 1;
                        this.loadTableData(event);
                    },                     
                    loadTableData:function(event){                          
                        httpPost(apiRolePage(this,router.authInfo,JSON.stringify(this.searchData))).success(function(res,context){ 
                            parseAndCheckResult(context,res);
                        }).then(function(status, res, context){context.$message.error(res);});
                    },
                    handleSizeChange(val) {                     
                        this.searchData.pageSize = val;
                        this.loadTableData();
                    },
                    handleCurrentChange(val) {
                        this.searchData.pageNum = val;
                        this.loadTableData();
                    },                  
                    handleEdit(row){
                        this.dialogForm = {
                            id:row.id, 
                            roleName:row.roleName, 
                            roleDescription:row.roleDescription,                                
                            status:row.status
                        };                       
                        this.dialogTitle = "Update Role";
                        this.dialogMode = 0;
                        this.editDialogFormVisible = true;
                    },                   
                    handleAdd(){
                        this.dialogForm = {
                            id:0,
                            roleName:"",
                            roleDescription:"",                                                                            
                            status:0
                        };                           
                        this.dialogTitle = "Add New Role";
                        this.dialogMode = 1;
                        this.editDialogFormVisible = true;
                    },
                    editSubmit:function(event){
                        this.editDialogFormVisible = false;                      
                        var apiRequest = (this.dialogMode)?apiRoleAdd(this,router.authInfo,JSON.stringify(this.dialogForm)):apiRoleUpdate(this,router.authInfo,JSON.stringify(this.dialogForm));
                        httpPost(apiRequest).success(function(res,context){                          
                            checkResult(context,res,false);                         
                            context.loadTableData();
                        }).then(function(status, res, context){checkResult(context,res,true);});
                    }
                }
            }); queryApp.onSubmit();//init end.
        } 

        function parseAndCheckResult(vApp, res){
            var resObj = JSON.parse(res); 
            if(resObj.code != SUCCESS_CODE){
                return false;
            }
            if(resObj.pageInfo){
                vApp.pageInfo.total = resObj.pageInfo.total;
                vApp.pageInfo.totalPage = resObj.pageInfo.totalPage;
            }
            if(resObj.data){                
                var arrayObj = new Array();
                for(var item of resObj.data){
                    arrayObj.push({
                        id:item.id,
                        roleName:item.roleName,                        
                        roleDescription:item.roleDescription,                                                           
                        status:item.deleted,
                        statusName:(item.deleted==1)?"disable":"normal"});
                }
                vApp.tableData = arrayObj;
            }
            return resObj;
        }

        function checkResult(vApp, res, isErr){            
            if(isErr){
                vApp.$message.error(res);
                return;
            }
            var resObj = JSON.parse(res); 
            if(resObj.code == SUCCESS_CODE){
                vApp.$message({type: 'success', message: resObj.message});
                return;
            }
            vApp.$message.error(resObj.message+","+resObj.code);
        }
    </script>
</body>
</html>
